/**
 * 作者: wgx
 * 日期: 2025-03-04 17:53:17
 * 备注: 用户子嗣匹配到的机器人信息
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.logic.ChildManager;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.redis.RedisTools;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;

public class UserMarryRobot extends UserMarryRobotModel {
    public UserMarryRobot() {
        super();
    }

    public UserMarryRobot(long childId, long userId, long earnAdd) {
        super();
        putChildId(childId);
        putUserId(userId);
        putEarnAdd(earnAdd);
        putAddTime(TimeUtils.getCurTime());
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public JsonObject insert() {
        // 如果超过上限则会删除最早的那条
        long count = Future.await(RedisTools.getHashLen(getListKey()));
        if (count >= AllParam.CHILD_MARRY_MATCH_INFO_MAX) {
            String orderListKey = EntityManager.getOrderListKey(getListKey());
            String delRecord = Future.await(RedisTools.lpop(orderListKey));// 删除列表数据
            long id = Long.parseLong(delRecord);// dbid
            UserMarryRobot userMarryRobot = ChildManager.getUserMarryRobot(getChildId(), id);
            if (userMarryRobot != null) {
                userMarryRobot.delete();
            }
        }
        return super.insert();
    }
}
